<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-4-26
 * Time: 下午1:36
 */

class shop extends adminBase{
    function __construct(){
        parent::__construct();
        $this->cms->setPageTitle("管理面板");
        $this->cms->setActionTitle("商家管理");
        $this->cms->setControlFile("admin/tpl/shop/shop_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(3,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page,$company,$real_name,$tel,$type){
        $shop=ShopMode::init();
        $url=WebRouter::init()->getQuestion("page=");
        $pageSize=20;
        $page=isset($page)&&($page>0)? (int)$page:1;
        $offset=((int)$page==1)? '0':($page-1)*$pageSize;
        if(!isset($company)&&!isset($real_name)&&!isset($tel)){
            if($type == 'checked'){
                list($r['list'],$shop_total)=$shop->getList(1,$pageSize,$offset);
            }else if($type == 'uncheck'){
                list($r['list'],$shop_total)=$shop->getList(0,$pageSize,$offset);
            }else{
                list($r['list'],$shop_total)=$shop->getList(2,$pageSize,$offset);
            }
            $r['pages']=new Page($page,$shop_total,$pageSize);
            $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        }else {
            $r['search']=$shop->getShopInfoBySearch($company,$real_name,$tel);
        }
        $this->cms->tableScene($r,"admin/tpl/shop/shop.php");
    }

    function checkPassTask($id){
        $shop=ShopMode::init();
        if($shop->auth($id)){
            header("Location:".WebRouter::init()->getPage("shop","index"));
        }else{
            $r['message']="操作失败，请重试！";
            $r['url']=WebRouter::init()->getAction("index");
            View::displayAsHtml($r,"plugin/state/tips.php");
        }
    }

    function checkCancelTask($id){
        $shop=ShopMode::init();
        if($shop->auth($id,0)){
            header("Location:".WebRouter::init()->getPage("shop","index"));
        }else{
            $r['message']="操作失败，请重试！";
            $r['url']=WebRouter::init()->getAction("index");
            View::displayAsHtml($r,"plugin/state/tips.php");
        }
    }

    function detailTask($id){
        $shop=ShopMode::init();
        $r=$shop->getDetail($id);
        $pic=PictureModule::init('picture');
        $address=AddressMode::init();
        $r['shop_address']=$address->getDetailAsString($r['address_id']);
        $company_pic=$pic->getDetail($r['company_pic']);
        $r['company_pic']=!empty($company_pic)?$company_pic:'upload/thumb.jpg';
        $license_pic=$pic->getDetail($r['license_pic']);
        $r['license_pic']=!empty($license_pic)?$license_pic:'upload/thumb.jpg';
        $idnum_pic=$pic->getDetail($r['idnum_pic']);
        $r['idnum_pic']=!empty($idnum_pic)?$idnum_pic:'upload/thumb.jpg';
        $recent_pic=$pic->getDetail($r['recent_pic']);
        $r['recent_pic']=!empty($recent_pic)?$recent_pic:'upload/thumb.jpg';
        $this->cms->normalScene($r,"admin/tpl/shop/shopDetail.php",
            CmsView::TYPE_FORM| CmsView::TYPE_JQUERY| CmsView::TYPE_EDITOR);
    }

    function shopModifyTask($id){
        $shop=ShopMode::init();
        $r=$shop->getDetail($id);
        $address=AddressMode::init();
        $pic=PictureModule::init('picture');
        $company_pic=$pic->getDetail($r['company_pic']);
        $r['company_pic']=!empty($company_pic)?$company_pic:'upload/thumb.jpg';
        $license_pic=$pic->getDetail($r['license_pic']);
        $r['license_pic']=!empty($license_pic)?$license_pic:'upload/thumb.jpg';
        $idnum_pic=$pic->getDetail($r['idnum_pic']);
        $r['idnum_pic']=!empty($idnum_pic)?$idnum_pic:'upload/thumb.jpg';
        $recent_pic=$pic->getDetail($r['recent_pic']);
        $r['recent_pic']=!empty($recent_pic)?$recent_pic:'upload/thumb.jpg';
        $r['shop_address']=$address->getDetailAsString($r['address_id']);
        $this->cms->normalScene($r,"admin/tpl/shop/shopModify.php",
            CmsView::TYPE_FORM| CmsView::TYPE_JQUERY| CmsView::TYPE_EDITOR);
    }

    function shopModifySubmitTask($id,$company,$address,$sex,$real_name,$birth,$tel,$email,$academic,
                                  $idnum,$idnum_pic,$license_pic,$company_pic,$computer_skill,
                                  $recent_pic,$address_id, $address_text,$nation,$qq,
                                  $visible=0,$imageInput,$introduction,$longitude,$latitude){
        $shop=ShopMode::init();
        $s=$shop->modify($id, $real_name, $sex,$tel, $academic,$birth, $idnum, $idnum_pic,
            $company, $license_pic, $company_pic, $nation,$qq, $email,
            $address, $computer_skill, $recent_pic, $introduction,
            $address_id, $address_text, $visible, $longitude, $latitude);
        if($s){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="商家信息修改成功";
        }else{
            $r['message']="修改失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function shopDeleteTask($id){
        $shop=ShopMode::init();
        $service=ServiceMode::init();
        $user=UserModule::init('user');
        $user_info=$shop->getDetail($id);
        $service_id=$service->getServiceId($user_info['user_id']);
        if(isset($service_id)){
            $user->setType($user_info['user_id'],'service');
        }else{
            $user->setType($user_info['user_id'],'user');
        }
        if($shop->delete($id)){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="删除成功";
        }else{
            $r['message']="删除失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

} 